/**
 * Created by EEO on 2017/7/1.
 */
require(['init'], function () {
    var studentuid = location.search.urlToObj().studentuid;
    var classname = location.search.urlToObj().name;
    var classid =location.search.urlToObj().id;
    console.log(window.location.href)
    $('.classname').text(classname);
    $('.classname').attr('href','./class_details.html?classuid='+classid+'&name='+$('.classname').text()+'')
//个人信息
    getstudentinformation();
    function getstudentinformation(){
        $.ajax({
            url:"/api/student/"+studentuid,
            method:"get",
            success:function(data){
                readerget(data.data)
            }
        })
    }
    function  readerget(data){
        function computeAge(startDate) {
            // 获得今天的时间
            var date = new Date();
            startDate = new Date(startDate);
            var newDate = date.getTime() - startDate.getTime();
            // 向下取整  例如 10岁 20天 会计算成 10岁
            // 如果要向上取整 计算成11岁，把floor替换成 ceil
            return Math.floor(newDate / 1000 / 60 / 60 / 24 / 365);
        }
        var age;
        age=computeAge(new Date(data.birthday).Format('YYYY-MM-DD'));
        if(data.birthday==null)age='';
        var sex;
        sex = data.sex == '0' ? i18next.t('sex.man'): i18next.t('sex.woman');
        if(data.sex==null)sex='';
      /*  console.log(ages(new Date(data.birthday).Format('YYYY-MM-DD')))
        console.log(new Date(data.birthday).Format('YYYY-MM-DD'));*/
        var $student_information=$("#student_information").html("");
        $('<div class="formLi">\
                <div class="formTitle ">\
                    <span>'+i18next.t('studentinfo.name')+'</span>\
                </div>\
                <div class="formContent">\
                    <span class="form-control" style="colo:#000">'+data.name+'</span>\
                </div>\
        </div>\
          <div class="formLi">\
                <div class="formTitle ">\
                    <span>'+i18next.t('studentinfo.sex')+'</span>\
                </div>\
                <div class="formContent">\
                  <span class="form-control" style="colo:#000">'+sex+'</span>\
                </div>\
          </div>\
         <div class="formLi">\
                <div class="formTitle ">\
                    <span>'+i18next.t('studentinfo.age')+'</span>\
                </div>\
                <div class="formContent">\
                  <span class="form-control" style="colo:#000">'+age+'</span>\
                </div>\
          </div>\
        <div class="formLi">\
                <div class="formTitle ">\
                    <span>'+i18next.t('studentinfo.birth')+'</span>\
                </div>\
                <div class="formContent">\
                  <span class="form-control" style="colo:#000">'+(data.birthday==null?" ":(data.birthday.substr(0,11)))+'</span>\
                </div>\
          </div>\
          <div class="formLi">\
              <div class="formTitle">\
                  <span>'+i18next.t('studentinfo.selfevaluation')+'</span>\
              </div>\
              <div class="formContent">\
                <span class="form-control"  style="height:100px;word-break: break-all;colo:#000" >'+(data.remark==null?" ":data.remark)+'</span>\
              </div>\
          </div>\
          </div>\
        </div>').appendTo($student_information);
    }
//上课情况
    classsituation();
    function classsituation() {
        $.ajax({
            url:"/api/teacher/student/"+studentuid,
            method:"get",
            success:function(data){
                bindhtml(data.data)
            }
        })
    }
    function bindhtml(data) {
        var $classlistTable = $('#J_classlist').html('');
        var $thead = $('<thead>\
          <tr class="enroll">\
            <th>'+i18next.t('table.no')+'</th>\
            <th>'+i18next.t('table.coursetype')+'</th>\
            <th>'+i18next.t('table.lessonsname')+'</th>\
            <th>'+i18next.t('table.coursedates')+'</th>\
            <th>'+i18next.t('table.schooltime')+'</th>\
            <th>'+i18next.t('table.duration')+'</th>\
            <th>'+i18next.t('table.firstclassroomtime')+'</th>\
            <th>'+i18next.t('table.lastclassroomtime')+'</th>\
            <th>'+i18next.t('table.classroomtime')+'</th>\
          </tr>\
        </thead>').appendTo($classlistTable);
        var $tbody = $('<tbody class="student_list"></tbody>').appendTo($classlistTable);
        function getDurationMinute(begintime, endtime) {
            if (begintime != null || endtime != null) {
                var begintime = +new Date(begintime.replace(' ', ' '));
                var endtime = +new Date(endtime.replace(' ', ' '));
                return (endtime - begintime) / (60 * 1000) + '分钟';
            } else {
                return '--';
            }
        }
        $.each(data, function (index, item) {
            console.log(item)
            var btimel="",btime="";
            if(item.begintime!="null"){
                btimel = item.begintime.substr(0,10);
                btime = item.begintime.substr(11,5);
            }
            var a=JSON.stringify(item.begintime);
            var day=(a).replace(/\-/g,"");
            if(item.eeosync!=null){
                var nn =  Math.floor(item.eeosync.studyTime/60)
            }
            if(nn!=0){
                nn=nn+'分钟'
            }
            var $classItem = $('<tr data-id='+day+' class="enroll"></tr>');
            $classItem.append('<td>' + (index + 1) + '</td>');
            $classItem.append('<td>' + item.coursetypename + '</td>');
            $classItem.append('<td>' + item.name + '</td>');
            $classItem.append('<td>' + btimel + '</td>');
            $classItem.append('<td>' + btime + '</td>');
            $classItem.append('<td>'+getDurationMinute(item.begintime, item.endtime)+'</td>');
            $classItem.append('<td>' +  (item.eeosync == null ? "--" : item.eeosync.firstEnterTime) + '</td>');
            $classItem.append('<td>' +  (item.eeosync == null ? "--" : item.eeosync.lastExitTime) + '</td>');
            $classItem.append('<td>' +  (item.eeosync == null ? "-- " : nn )+'</td>');
            $tbody.append($classItem);
            $tbody.find("tr").each(function () {
                //console.log(this)
                var tdhtml = $(this).children();
                $.each(tdhtml, function (i, t) {
                    $(t).text() == 'null' ? $(t).text('--') : $(t).text()
                    $(t).text() == 'undefined' ? $(t).text('--') : $(t).text()
                    $(t).text() == '0000-00-00' ? $(t).text('--') : $(t).text()
                    $(t).text() == '0000-00-00 00:00:00' ? $(t).text('--') : $(t).text()
                    $(t).text() == '0' ? $(t).text('--') : $(t).text()
                    $(t).text() == 'NaNaN-aN-aN' ? $(t).text('--') : $(t).text()
                    $(t).text() == '' ? $(t).text('--') : $(t).text()
                })
            });

        })
    }

});








